/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#if !defined(AFX_PGLSTOCK_H__1842A02E_B6B2_43EF_B696_7D26436D362B__INCLUDED_)
#define AFX_PGLSTOCK_H__1842A02E_B6B2_43EF_B696_7D26436D362B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <pgl/core/PGLColor.h>
#include <pgl/core/PGLLine.h>

/*! \brief A Open, High, Low, Close Stock chart

\par Data
  The user must specify:

	- x0, starting x coordinate, (SetX0)
	- dx, steps between measurements, (SetDx)
	- vOpens, vHighs, vLows and vCloses (SetDatas),

If the vectors have not the same size, PGL consider the minimum size. Nothing is drawed for number of points < 2.

\par Colors


  \image html stockexplain.png "Description of a stock, high - low - close chart"
  \ingroup PGLLineGroup
*/
class PGLCORE_EXT_CLASS CPGLStock : public CPGLLine  
{
PGL_DECLARE_DYNCREATE(CPGLStock);
public:
	//! Type of stock chart
	enum EType
	{
		//! Candle stick chart
		TypeCandleStick	=	0x0001,
		//! Open high  low close
		TypeOpenHighLowClose = 0x0002
	};

    //! \name Constructor
    //@{
    //! default constructor
    CPGLStock(const CPGLColor& _FrontColor = CPGLColor(0,0,0,1), const CPGLColor& _FillColor = CPGLColor(1,1,1,1));
    //! copy constructor
    CPGLStock(const CPGLStock& l){*this = l;};
    //! assignement constructor
    CPGLStock& operator=(const CPGLStock& l);
    //! desctructor
	virtual ~CPGLStock();
	//@}

	//! \name Extent computation
	//@{
	//! Extent computation
    virtual void GetUnionExtent(gfxinterface::CGfxInterface& gfx, CPGLRectD& rExtent);
    ///virtual function to override in derived classes
    virtual void UpdateExtent(gfxinterface::CGfxInterface& gfx);
	//@}
		
	//! \name GUI
	//@{
    //! adds the Line2D property page to the property dialog
//    virtual void AddPropertyPage(CPropertySheet* pPropSheet);
	//@}

	//! \name Type
	//@{
	//! returns type
	DWORD GetType() const			{	return m_dwType;};
	//! sets type
	void SetType( DWORD _dwType)	{	m_dwType = _dwType;};
	//@}

	//! \name Colors
	//@{
	//! Sets rising color
	void SetUpColor(const CPGLColor& c)	{	SetColor(c);};
	//! returns increasing color
	CPGLColor& GetUpColor()				{	return GetColor();};
	//! returns increasing color, const
	const CPGLColor& GetUpColor() const	{	return GetColor();};
	//! Sets decreasing color
	void SetDownColor(const CPGLColor& c)	{	m_cDown = c;};
	//! return decreasing color
	CPGLColor& GetDownColor()				{	return m_cDown;};
	//! return decreasing color, const
	const CPGLColor& GetDownColor() const	{	return m_cDown;};
	//@}

	//!  \name Data manipulation
	//@{
	double GetX0() const	{	return m_dX0;};
	void SetX0(double _dX0)	{	m_dX0 = _dX0;};
	double GetDx() const	{	return m_dDx;};
	void SetDx(double _dDx)	{	m_dDx = _dDx;};
	//! returns number of entries
	size_t GetNPoints() const	{	ASSERT(m_pHis); ASSERT(m_pLows); ASSERT(m_pCloses);
		return __min(m_pHis->size(), __min( m_pLows->size(), m_pCloses->size()) );};
	//! returns the "highs" entries, const
	const CPGLDataPtr GetHis() const		{	return m_pHis;};
	//! returns the "lows" entries, const
	const CPGLDataPtr GetLows() const		{	return m_pLows;};
	//! return the "open" entries, const
	const CPGLDataPtr GetOpens() const	{	return m_pOpens;};
	//! returns the "closes" entries, const
	const CPGLDataPtr GetCloses() const	{	return m_pCloses;};
	//! returns the "highs" entries
	CPGLDataPtr GetHis()		{	return m_pHis;};
	//! returns the "lows" entries
	CPGLDataPtr GetLows()		{	return m_pLows;};
	//! return the "open" entries
	CPGLDataPtr GetOpens()	{	return m_pOpens;};
	//! returns the "closes" entries
	CPGLDataPtr GetCloses()	{	return m_pCloses;};

    //! Generic data setting function
    virtual void SetDatas(CPGLDataPtr pHis, CPGLDataPtr pLows, CPGLDataPtr pOpens, CPGLDataPtr pCloses);

	/*! Setting data as vector of float

	This is a helper function that uses the generic form of SetDatas and spares the user the creation of CPGLData objects.
	\param bMakeOwnCopy if true, a copy of the data is stored, otherwize only the reference is stored (default is true)
	*/
    virtual void SetDatas(const std::vector<float>& _vHis, const std::vector<float>& _vLows, const std::vector<float>& _vOpens, const std::vector<float>& _vCloses, bool bMakeOwnCopy = true);
	//! Clears the data vectors
	void ClearData();
	//@}

	//! \name Serialization and debugging
	//@{
	//! Saving setttings to XML
	void SerializeXML(CPGLArchive& ar);
    //! Debuggin functions
    #ifdef _DEBUG
        virtual void Dump( CDumpContext& dc ) const;
        virtual void AssertValid() const;    // Override
    #endif
    //@}
protected:
	//! \name Virtual functions override
	//@{
	virtual void PlotLineStripGfx(gfxinterface::CGfxInterface& gfx);
	//@}

protected:
	DWORD GetEStockType();
	//! \name Protected attributes
	//@{
	//! First coordinate of data
	double m_dX0;
	//! spatial step
	double m_dDx;
	//! hi's
	CPGLDataPtr m_pHis;
	//! lo's
	CPGLDataPtr m_pLows;
	//! Openings
	CPGLDataPtr m_pOpens;
	//! Close's
	CPGLDataPtr m_pCloses;
	//! Stock type
	DWORD m_dwType;
	//! decreasing color
	CPGLColor m_cDown;
	//@}
private:
	void ResetData();
	CPGLRectU m_rLimitIndices;
};

PGL_DECLARE_SMARTPTR( CPGLStock );

/*! \example stock.cpp

This example demonstrate the stock (high - low - close) chart.
\image html stock.png "Stock chart"
*/
#endif // !defined(AFX_PGLSTOCK_H__1842A02E_B6B2_43EF_B696_7D26436D362B__INCLUDED_)
